package com.bo.gather.rule;

import java.util.List;

import com.bo.gather.bean.Lengthinfo;
import com.bo.gather.bean.MustIncludeInfo;
import com.bo.gather.bean.RemoveInfo;
import com.bo.gather.bean.ReplaceInfo;
import com.bo.gather.bean.weibo.IncludeNotFileInfo;
import com.bo.gather.bean.weibo.OrderLengthinfo;

public class RuleAnalyse {
	public static String returnpass="OK";
	public final static String delete="delete";
	public final static String alldelete="alldelete";
	public static String calculateIncludeNotFile(String content ,IncludeNotFileInfo includeNotFileInfo,String title){
		for(String s:includeNotFileInfo.getCleanList()){
			content=content.replaceAll(s, "");
		}
		
		for(String s:includeNotFileInfo.getCleanList()){
			title=title.replaceAll(s, "");
		}
		if(content.length()<=title.length()){
			if(title.indexOf(content)!=-1){
				return RuleAnalyse.delete;
			}
		}
		return RuleAnalyse.returnpass;
	}
	
	/**
	 * {length-order:content>100;clean:[0-9]clean:[a-y]clean:[A-Y]clean:\\pP|\\pSclean:" "}
	 * @param htmlcontent
	 * @param lengthinfo
	 * @return
	 */
	public static String calculateOrderLength(String htmlcontent,OrderLengthinfo lengthinfo){
		if(lengthinfo!=null){
			for(String s:lengthinfo.getCleanList()){
				htmlcontent=htmlcontent.replaceAll(s, "");
			}
			String ifinfo=lengthinfo.getIfinfo();
			int length=lengthinfo.getLength();
			if(length>0){
				boolean bo=false;
				if(">=".equals(ifinfo)){
					bo=htmlcontent.length()>=length;
				}else if("<=".equals(ifinfo)){
					bo=htmlcontent.length()<=length;
				}else if(">".equals(ifinfo)){
					bo=htmlcontent.length()>length;
				}else if("<".equals(ifinfo)){
					bo=htmlcontent.length()<length;
				}
				if(bo){
					return RuleAnalyse.returnpass;
				}
			}
		}
		return lengthinfo.getIsDetele();
	}
	/**
	 * 
	 * @param htmlcontent
	 * @param lengthinfo
	 * @return
	 */
	public static String calculateLength(String htmlcontent,Lengthinfo lengthinfo){
		if(lengthinfo!=null){
			for(String s:lengthinfo.getCleanList()){
				htmlcontent=htmlcontent.replaceAll(s, "");
			}
			String ifinfo=lengthinfo.getIfinfo();
			int length=lengthinfo.getLength();
			if(length>0){
				boolean bo=false;
				if(">=".equals(ifinfo)){
					bo=htmlcontent.length()>=length;
				}else if("<=".equals(ifinfo)){
					bo=htmlcontent.length()<=length;
				}else if(">".equals(ifinfo)){
					bo=htmlcontent.length()>length;
				}else if("<".equals(ifinfo)){
					bo=htmlcontent.length()<length;
				}
				if(bo){
					return lengthinfo.getIsDetele();
				}
			}
		}
		return RuleAnalyse.returnpass;
	}
	
	public static String calculateRemove(String htmlcontent,List<RemoveInfo> removeList){
		for(RemoveInfo remove:removeList){
			if(remove.getRemove().equals("") && htmlcontent.equals(remove.getRemove())){
				return remove.getIsDetele();
			}
			if(!remove.getRemove().equals("")){
				int index=htmlcontent.indexOf(remove.getRemove());
				if(index!=-1){
					return remove.getIsDetele();
				}
			}
		}
		return RuleAnalyse.returnpass;
	}
	/**
	 * 计算必须包含
	 * @param htmlcontent
	 * @param mustList
	 * @return
	 */
	public static String calculateMustInfo(String htmlcontent,List<MustIncludeInfo> mustList){
		for(MustIncludeInfo must:mustList){
			int index=htmlcontent.indexOf(must.getMustinfo());
			if(index==-1){
				return must.getIsDetele();
			}
		}
		return RuleAnalyse.returnpass;
	}
	public static String calculateReplace(String htmlcontent,List<ReplaceInfo> replaceList){
		for(ReplaceInfo replace:replaceList){
			//这种情况会导致{remove:QQ@@alldelete} replace.getNewStr()为null
			if(replace.getOldStr()!=null && replace.getNewStr()==null){
				replace.setNewStr("");
			}
			htmlcontent=htmlcontent.replaceAll(replace.getOldStr(), replace.getNewStr());
		}
		return htmlcontent;
	}
}
